home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Deflater.java < prev    next >
Text File  |  1998-09-22  |  10KB  |  307 lines

  1. /*
  2.  * @(#)Deflater.java    1.19 98/08/20
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.util.zip;
  24.  
  25. /**
  26.  * This class provides support for general purpose decompression using
  27.  * the popular ZLIB compression library. The ZLIB compression library
  28.  * was initially developed as part of the PNG graphics standard and is
  29.  * not protected by patents. It is fully described in RFCs 1950, 1951,
  30.  * and 1952, which can be found at 
  31.  * <a href="http://info.internet.isi.edu:80/in-notes/rfc/files/">
  32.  * http://info.internet.isi.edu:80/in-notes/rfc/files/
  33.  * </a> in the files rfc1950.txt (zlib format),
  34.  * rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  35.  * 
  36.  * @see        Inflater
  37.  * @version     1.19, 08/20/98
  38.  * @author     David Connelly
  39.  */
  40. public
  41. class Deflater {
  42.     private int strm;
  43.     private byte[] buf = new byte[0];
  44.     private int off, len;
  45.     private int level, strategy;
  46.     private boolean setParams;
  47.     private boolean finish, finished;
  48.  
  49.     /**
  50.      * Compression method for the deflate algorithm (the only one currently
  51.      * supported).
  52.      */
  53.     public static final int DEFLATED = 8;
  54.  
  55.     /**
  56.      * Compression level for no compression.
  57.      */
  58.     public static final int NO_COMPRESSION = 0;
  59.  
  60.     /**
  61.      * Compression level for fastest compression.
  62.      */
  63.     public static final int BEST_SPEED = 1;
  64.  
  65.     /**
  66.      * Compression level for best compression.
  67.      */
  68.     public static final int BEST_COMPRESSION = 9;
  69.  
  70.     /**
  71.      * Default compression level.
  72.      */
  73.     public static final int DEFAULT_COMPRESSION = -1;
  74.  
  75.     /**
  76.      * Compression strategy best used for data consisting mostly of small
  77.      * values with a somewhat random distribution. Forces more Huffman coding
  78.      * and less string matching.
  79.      */
  80.     public static final int FILTERED = 1;
  81.  
  82.     /**
  83.      * Compression strategy for Huffman coding only.
  84.      */
  85.     public static final int HUFFMAN_ONLY = 2;
  86.  
  87.     /**
  88.      * Default compression strategy.
  89.      */
  90.     public static final int DEFAULT_STRATEGY = 0;
  91.  
  92.     /*
  93.      * Loads the ZLIB library.
  94.      */
  95.     static {
  96.     System.loadLibrary("zip");
  97.     }
  98.  
  99.     /**
  100.      * Creates a new compressor using the specified compression level.
  101.      * If 'nowrap' is true then the ZLIB header and checksum fields will
  102.      * not be used in order to support the compression format used in
  103.      * both GZIP and PKZIP.
  104.      * @param level the compression level (0-9)
  105.      * @param nowrap if true then use GZIP compatible compression
  106.      */
  107.     public Deflater(int level, boolean nowrap) {
  108.     this.level = level;
  109.     this.strategy = DEFAULT_STRATEGY;
  110.     init(nowrap);
  111.     }
  112.  
  113.     /** 
  114.      * Creates a new compressor using the specified compression level.
  115.      * Compressed data will be generated in ZLIB format.
  116.      * @param level the compression level (0-9)
  117.      */
  118.     public Deflater(int level) {
  119.     this(level, false);
  120.     }
  121.  
  122.     /**
  123.      * Creates a new compressor with the default compression level.
  124.      * Compressed data will be generated in ZLIB format.
  125.      */
  126.     public Deflater() {
  127.     this(DEFAULT_COMPRESSION, false);
  128.     }
  129.  
  130.     /**
  131.      * Sets input data for compression. This should be called whenever
  132.      * needsInput() returns true indicating that more input data is required.
  133.      * @param b the input data bytes
  134.      * @param off the start offset of the data
  135.      * @param len the length of the data
  136.      * @see Deflater#needsInput
  137.      */
  138.     public synchronized void setInput(byte[] b, int off, int len) {
  139.     if (b== null) {
  140.         throw new NullPointerException();
  141.     }
  142.     if (off < 0 || len < 0 || off + len > b.length) {
  143.         throw new ArrayIndexOutOfBoundsException();
  144.     }
  145.     this.buf = b;
  146.     this.off = off;
  147.     this.len = len;
  148.     }
  149.  
  150.     /**
  151.      * Sets input data for compression. This should be called whenever
  152.      * needsInput() returns true indicating that more input data is required.
  153.      * @param b the input data bytes
  154.      * @see Deflater#needsInput
  155.      */
  156.     public void setInput(byte[] b) {
  157.     setInput(b);
  158.     }
  159.  
  160.     /**
  161.      * Sets preset dictionary for compression. A preset dictionary is used
  162.      * when the history buffer can be predetermined. When the data is later
  163.      * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
  164.      * in order to get the Adler-32 value of the dictionary required for
  165.      * decompression.
  166.      * @param b the dictionary data bytes
  167.      * @param off the start offset of the data
  168.      * @param len the length of the data
  169.      * @see Inflater#inflate
  170.      * @see Inflater#getAdler
  171.      */
  172.     public synchronized native void setDictionary(byte[] b, int off, int len);
  173.  
  174.     /**
  175.      * Sets preset dictionary for compression. A preset dictionary is used
  176.      * when the history buffer can be predetermined. When the data is later
  177.      * uncompressed with Inflater.inflate(), Inflater.getAdler() can be called
  178.      * in order to get the Adler-32 value of the dictionary required for
  179.      * decompression.
  180.      * @param b the dictionary data bytes
  181.      * @see Inflater#inflate
  182.      * @see Inflater#getAdler
  183.      */
  184.     public void setDictionary(byte[] b) {
  185.     setDictionary(b, 0, b.length);
  186.     }
  187.  
  188.     /**
  189.      * Sets the compression strategy to the specified value.
  190.      * @param strategy the new compression strategy
  191.      * @exception IllegalArgumentException if the compression strategy is
  192.      *                           invalid
  193.      */
  194.     public synchronized void setStrategy(int strategy) {
  195.     switch (strategy) {
  196.       case DEFAULT_STRATEGY:
  197.       case FILTERED:
  198.       case HUFFMAN_ONLY:
  199.         break;
  200.       default:
  201.         throw new IllegalArgumentException();
  202.     }
  203.     if (this.strategy != strategy) {
  204.         this.strategy = strategy;
  205.         setParams = true;
  206.     }
  207.     }
  208.  
  209.     /**
  210.      * Sets the current compression level to the specified value.
  211.      * @param level the new compression level (0-9)
  212.      * @exception IllegalArgumentException if the compression level is invalid
  213.      */
  214.     public synchronized void setLevel(int level) {
  215.     if ((level < 0 || level > 9) && level != DEFAULT_COMPRESSION) {
  216.         throw new IllegalArgumentException("invalid compression level");
  217.     }
  218.     if (this.level != level) {
  219.         this.level = level;
  220.         setParams = true;
  221.     }
  222.     }
  223.  
  224.     /**
  225.      * Returns true if the input data buffer is empty and setInput()
  226.      * should be called in order to provide more input.
  227.      */
  228.     public boolean needsInput() {
  229.     return len <= 0;
  230.     }
  231.  
  232.     /**
  233.      * When called, indicates that compression should end with the current
  234.      * contents of the input buffer.
  235.      */
  236.     public synchronized void finish() {
  237.     finish = true;
  238.     }
  239.  
  240.     /**
  241.      * Returns true if the end of the compressed data output stream has
  242.      * been reached.
  243.      */
  244.     public synchronized boolean finished() {
  245.     return finished;
  246.     }
  247.  
  248.     /**
  249.      * Fills specified buffer with compressed data. Returns actual number
  250.      * of bytes of compressed data. A return value of 0 indicates that
  251.      * needsInput() should be called in order to determine if more input
  252.      * data is required.
  253.      * @param b the buffer for the compressed data
  254.      * @param off the start offset of the data
  255.      * @param len the maximum number of bytes of compressed data
  256.      * @return the actual number of bytes of compressed data
  257.      */
  258.     public synchronized native int deflate(byte[] b, int off, int len);
  259.  
  260.     /**
  261.      * Fills specified buffer with compressed data. Returns actual number
  262.      * of bytes of compressed data. A return value of 0 indicates that
  263.      * needsInput() should be called in order to determine if more input
  264.      * data is required.
  265.      * @param b the buffer for the compressed data
  266.      * @return the actual number of bytes of compressed data
  267.      */
  268.     public int deflate(byte[] b) {
  269.     return deflate(b, 0, b.length);
  270.     }
  271.  
  272.     /**
  273.      * Returns the ADLER-32 value of the uncompressed data.
  274.      */
  275.     public synchronized native int getAdler();
  276.  
  277.     /**
  278.      * Returns the total number of bytes input so far.
  279.      */
  280.     public synchronized native int getTotalIn();
  281.  
  282.     /**
  283.      * Returns the total number of bytes output so far.
  284.      */
  285.     public synchronized native int getTotalOut();
  286.  
  287.     /**
  288.      * Resets deflater so that a new set of input data can be processed.
  289.      * Keeps current compression level and strategy settings.
  290.      */
  291.     public synchronized native void reset();
  292.  
  293.     /**
  294.      * Discards unprocessed input and frees internal data.
  295.      */
  296.     public synchronized native void end();
  297.  
  298.     /**
  299.      * Frees the compressor when garbage is collected.
  300.      */
  301.     protected void finalize() {
  302.     end();
  303.     }
  304.  
  305.     private native void init(boolean nowrap);
  306. }
  307.